package cn.im.push;

import cn.im.accid.CheckSumBuilder;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/9.
 */
public class SendMessage {

    public static final String appKey = "5ec38e64fd7a39b117e10780d1650377";
    public static final String appSecret = "ceb1e82b9c52";
    public static final String nonce =  "12345";
    //批量发送点对点普通消息 toaccids ["aaa","bbb"]
    public static void sendBatchMsg(String fromaccid,String toaccids,String msg,String msgtype) throws UnsupportedEncodingException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/nimserver/msg/sendBatchMsg.action";
        HttpPost httpPost = new HttpPost(url);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("fromAccid", fromaccid));//
        nvps.add(new BasicNameValuePair("toAccids", toaccids));
        nvps.add(new BasicNameValuePair("type", msgtype));//0文本 1图片
        nvps.add(new BasicNameValuePair("body", msg)); //json字符串
        nvps.add(new BasicNameValuePair("option", "{'route':false}")); //关闭消息抄送
        nvps.add(new BasicNameValuePair("pushcontent", msg)); //ios ，option 默认push为true ，此字段可推送
        nvps.add(new BasicNameValuePair("payload", msg)); //option 默认push为true ，
        //nvps.add(new BasicNameValuePair("ext", msg)); //ios 推送
        //返回说明 http 响应:json
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
    }
    //发送自定义系统通知
    public static void sendAttachMsg(String fromaccid,String toaccid,String msg,String msgtype) throws UnsupportedEncodingException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/nimserver/msg/sendAttachMsg.action";
        HttpPost httpPost = new HttpPost(url);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("from", fromaccid));//
        nvps.add(new BasicNameValuePair("to", toaccid));
        nvps.add(new BasicNameValuePair("msgtype", msgtype));//0文本 1图片
        nvps.add(new BasicNameValuePair("attach", msg)); //json字符串
        nvps.add(new BasicNameValuePair("pushcontent", msg)); //ios 推送
        // nvps.add(new BasicNameValuePair("ext", msg)); // 如果发送的图片，这里说明图片大小 后缀，高宽
        nvps.add(new BasicNameValuePair("save", "2")); //2 会存离线
        nvps.add(new BasicNameValuePair("option", "{'route':false}")); //关闭消息抄送
        //返回说明 http 响应:json
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
    }
    //批量发送点对点自定义系统通知
    public static void sendBatchAttachMsg(String fromaccid,String toaccids,String msg) throws UnsupportedEncodingException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/nimserver/msg/sendBatchAttachMsg.action";
        HttpPost httpPost = new HttpPost(url);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("fromAccid", fromaccid));//
        nvps.add(new BasicNameValuePair("toAccids", toaccids)); //json 字符串
        nvps.add(new BasicNameValuePair("attach", msg)); //json字符串
        nvps.add(new BasicNameValuePair("pushcontent", msg)); //ios 推送
        nvps.add(new BasicNameValuePair("payload", msg)); //ios 推送
        nvps.add(new BasicNameValuePair("save", "2")); //2表示会存离线
        nvps.add(new BasicNameValuePair("option", "{'route':false}")); //关闭消息抄送
        //返回说明 http 响应:json
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
    }
}
